use inequality_data_70s_correct , clear 

set more off
log using income_pctile , replace 
*drop years for which we do not have all observations
drop if ref_year==1979
drop if ref_year==2013
*limit to full income reports 
keep if respstat==1 

*ensure summation in Total Vehicle Cons works correctly
foreach var of varlist vehicpq vehiccq cartknpq cartkncq cartkupq cartkucq caropq carocq {
replace `var' = 0 if (`var'==. & ref_year>=1980) 
}

*Generate component totals to create different measures of consumption
* food at home
gen fdhome_tot = .
replace fdhome_tot = fdhome/4 if (ref_year<1970)
replace fdhome_tot = d_fdhome/4 if (ref_year>=1970 & ref_year<=1979) 
replace fdhome_tot = fdhomepq2+fdhomecq2 if (ref_year >= 1980)
*transportation 
gen trans_tot = .
replace trans_tot = (publictransother+automobileops+n_tot_vflow1)/4 if(ref_year<1970) 
replace trans_tot = (d_gas + d_othvehic-d_autofin+d_pubtrans+n_tot_vflow1)/4 if (ref_year>=1970 & ref_year<=1979)
replace trans_tot = (transpq+transcq+n_tot_vflow1-(vehfinpq+vehfincq+vehicpq+vehiccq+cartknpq+cartkncq+cartkupq+cartkucq+caropq+carocq)) if (ref_year>=1980)
*utilities 
gen util_tot = .
replace util_tot = utilites/4 if (ref_year<1970) 
replace util_tot = d_utility/4 if (ref_year>=1970 & ref_year<=1979) 
replace util_tot = utilpq+utilcq if (ref_year>=1980) 
*Adjusted utilities from predictions
*replace util_tot = n_adj_util_floor if (cutenure==4 & _merge_rnt==3)
*housing
gen hous_tot = . 
replace hous_tot = (totrentdwe +hflow)/4 if (ref_year<1970) 
replace hous_tot = (d_rent + hflow)/4 if (ref_year>=1970 & ref_year<=1979) 
replace hous_tot = rendwepq+rendwecq+hflow if (ref_year>=1980) 
*difference in adjusted and reported utilities is utility payment included in rent (forced to be positive) 
*gen diff_rnt = n_adj_util_floor - (utilpq+utilcq) if (cutenure==4 & _merge_rnt==3) 
*replace hous_tot = hous_tot - diff_rnt if (cutenure==4 & _merge_rnt==3 & diff_rnt>0 & diff_rnt!=.) 
*vehicles 
gen vehic_tot = . 
replace vehic_tot = n_tot_vflow1/4 if (ref_year<=1979)
replace vehic_tot = n_tot_vflow1 if (ref_year>=1980)

*Recode cons5 to drop all elements of transportation that are not well-measured
*AND to get rid of public housing from measure
rename cons5 uncorrected_cons
gen double cons5 = .
replace cons5= ((uncorrected_cons*scale)+(n_tot_vflow1/4)+gas_mo-trans_tot)/scale if (ref_year<1980)
replace cons5=((uncorrected_cons*scale)+n_tot_vflow1+gas_mo-trans_tot)/scale if (ref_year>=1980)
replace cons5= ((cons5*scale)-(tflow2-tflow))/scale if (ref_year>=1980) 

*Generate cpi defalator
gen cpi_u_rs_adj_11=.
replace cpi_u_rs_adj_11=4.263 if (ref_year==1960)
replace cpi_u_rs_adj_11=4.266 if (ref_year==1961)
replace cpi_u_rs_adj_11=4.27 if (ref_year==1962)
replace cpi_u_rs_adj_11=4.259 if (ref_year==1963)
replace cpi_u_rs_adj_11=4.248 if (ref_year==1964)
replace cpi_u_rs_adj_11=4.228 if (ref_year==1965)
replace cpi_u_rs_adj_11=4.156 if (ref_year==1966)
replace cpi_u_rs_adj_11=4.075 if (ref_year==1967)
replace cpi_u_rs_adj_11=3.962 if (ref_year==1968)
replace cpi_u_rs_adj_11=3.834 if (ref_year==1969)
replace cpi_u_rs_adj_11=3.693 if (ref_year==1970)
replace cpi_u_rs_adj_11=3.575 if (ref_year==1971)
replace cpi_u_rs_adj_11=3.509 if (ref_year==1972)
replace cpi_u_rs_adj_11=3.337 if (ref_year==1973)
replace cpi_u_rs_adj_11=3.064 if (ref_year==1974)
replace cpi_u_rs_adj_11=2.86 if (ref_year==1975)
replace cpi_u_rs_adj_11=2.733 if (ref_year==1976) 
replace cpi_u_rs_adj_11=2.597 if (ref_year==1977)
replace cpi_u_rs_adj_11=2.449 if (ref_year==1978)
replace cpi_u_rs_adj_11=2.252 if (ref_year==1979)
replace cpi_u_rs_adj_11=2.041 if (ref_year==1980) 
replace cpi_u_rs_adj_11=1.878 if (ref_year==1981)
replace cpi_u_rs_adj_11=1.784 if (ref_year==1982)
replace cpi_u_rs_adj_11=1.724 if (ref_year==1983)
replace cpi_u_rs_adj_11=1.669 if (ref_year==1984)
replace cpi_u_rs_adj_11=1.627 if (ref_year==1985)
replace cpi_u_rs_adj_11=1.61 if (ref_year==1986)
replace cpi_u_rs_adj_11=1.57 if (ref_year==1987) 
replace cpi_u_rs_adj_11=1.526 if (ref_year==1988)
replace cpi_u_rs_adj_11=1.474 if (ref_year==1989)
replace cpi_u_rs_adj_11=1.415 if (ref_year==1990)
replace cpi_u_rs_adj_11=1.377 if (ref_year==1991)
replace cpi_u_rs_adj_11=1.353 if (ref_year==1992)
replace cpi_u_rs_adj_11=1.331 if (ref_year==1993)
replace cpi_u_rs_adj_11=1.313 if (ref_year==1994)
replace cpi_u_rs_adj_11=1.293 if (ref_year==1995)
replace cpi_u_rs_adj_11=1.269 if (ref_year==1996)
replace cpi_u_rs_adj_11=1.252 if (ref_year==1997)
replace cpi_u_rs_adj_11=1.245 if (ref_year==1998)
replace cpi_u_rs_adj_11=1.229 if (ref_year==1999) 
replace cpi_u_rs_adj_11=1.198 if (ref_year==2000)
replace cpi_u_rs_adj_11=1.175 if (ref_year==2001)
replace cpi_u_rs_adj_11=1.165 if (ref_year==2002)
replace cpi_u_rs_adj_11=1.149 if (ref_year==2003)
replace cpi_u_rs_adj_11=1.127 if (ref_year==2004)
replace cpi_u_rs_adj_11=1.099 if (ref_year==2005)
replace cpi_u_rs_adj_11=1.073 if (ref_year==2006)
replace cpi_u_rs_adj_11=1.051 if (ref_year==2007)
replace cpi_u_rs_adj_11=1.02 if (ref_year==2008) 
replace cpi_u_rs_adj_11=1.032 if (ref_year==2009) 
replace cpi_u_rs_adj_11=1.024 if (ref_year==2010) 
replace cpi_u_rs_adj_11=1 if (ref_year==2011)
replace cpi_u_rs_adj_11=.987 if (ref_year==2012)

****CREATE COMPONENT PERCENTILES 
*Create transportation total variable with ONLY transportation expenditures included in core consumption measures
gen trans_tot1=.
replace trans_tot1 = (n_tot_vflow1/4)+gas_mo if (ref_year<=1979)
replace trans_tot1 = n_tot_vflow1+gas_mo if (ref_year>=1980)

*create housing + utilities expenditures measure 
gen hous_util_tot = hous_tot+util_tot

*scale all component measures 
gen trans_scle = trans_tot1/scale
gen hous_scle = hous_tot/scale
gen fdhome_scle = fdhome_tot/scale 
gen hous_util_scle = hous_util_tot/scale 
gen vehic_scle = vehic_tot/scale 

*convert to 2011 dollars 
foreach var of varlist trans_scle hous_scle fdhome_scle hous_util_scle vehic_scle {
gen r_`var'=cpi_u_rs_adj_11*`var' 
}
*Generate scaled income 
gen double fincbtax_scle = fincbtax/scale 

*Create indicators of percentile of core consumption by year
foreach year of numlist  1972 1973  1983/2012 {
foreach num of numlist 5 15 45 55 85 95 {
egen incpct_`num'_`year' = pctile(fincbtax_scle) if (ref_year==`year') , p(`num') 
}
}
gen str12 indicator=""
foreach year of numlist  1972 1973 1983/2012 {
replace indicator = "pct_10_`year'" if (ref_year==`year' & fincbtax_scle>=incpct_5_`year' & fincbtax_scle<=incpct_15_`year')
replace indicator = "pct_50_`year'" if (ref_year==`year' & fincbtax_scle>=incpct_45_`year' & fincbtax_scle<=incpct_55_`year')
replace indicator = "pct_90_`year'" if (ref_year==`year' & fincbtax_scle>=incpct_85_`year' & fincbtax_scle<=incpct_95_`year')
}
drop incpct_*

tempfile temp10 temp50

*Create indicator for house ownership
gen home_own = 0
replace home_own = 1 if (ref_year==1961 & cutenure==1) 
replace home_own = 1 if (ref_year>=1970 & ref_year!=. & (cutenure==1 | cutenure==2 | cutenure==3))
*create indicator for cars that will be consistent for all years 
gen vehq_cons = 0 if (vehq==0) 
replace vehq_cons = 1 if (vehq==1)
replace vehq_cons = 2 if (vehq>=2 & vehq!=.) 

foreach year of numlist  1972 1973 1983/2012 { 

preserve 
keep if indicator=="pct_10_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by (ref_year)
gen percentile=10
save "`temp10'" , replace 
restore 

preserve 
keep if indicator=="pct_50_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by(ref_year)
gen percentile=50
save "`temp50'" , replace 
restore 

preserve 
keep if indicator=="pct_90_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by(ref_year)
gen percentile=90

append using `temp50' 
append using `temp10'
save inc_pct`year' , replace 
restore 
}

use inc_pct1972 , clear 
foreach year of numlist  1973 1983/2012 {
append using inc_pct`year' 
}
sort ref_year percentile 
save inc_pct_allyears , replace 

*repeat for 60s
use inequality_data_for_60 , clear 
drop if (ref_year==1972 | ref_year==1973)
keep if respstat==1
****CREATE COMPONENT PERCENTILES 
*Create transportation total variable with ONLY transportation expenditures included in core consumption measures
gen trans_tot1=.
replace trans_tot1 = (n_tot_vflow1/4)+gas_mo if (ref_year<=1979)
replace trans_tot1 = n_tot_vflow1+gas_mo if (ref_year>=1980)

*create housing + utilities expenditures measure 
gen hous_util_tot = hous_tot+util_tot
*create vehicle total
gen vehic_tot =.
replace vehic_tot = n_tot_vflow1/4 if (ref_year<=1979)
replace vehic_tot = n_tot_vflow1 if (ref_year>=1980)

*scale all component measures 
gen trans_scle = trans_tot1/scale
gen hous_scle = hous_tot/scale
gen fdhome_scle = fdhome_tot/scale 
gen hous_util_scle = hous_util_tot/scale 
gen vehic_scle = vehic_tot/scale


*convert to 2011 dollars 
foreach var of varlist trans_scle hous_scle fdhome_scle hous_util_scle vehic_scle {
gen r_`var'=cpi_u_rs_adj_11*`var' 
}

*Generate scaled income 
gen double fincbtax_scle = fincbtax/scale 

*Create indicators of percentile of core consumption by year
foreach year of numlist 1961 {
foreach num of numlist 5 15 45 55 85 95 {
egen incpct_`num'_`year' = pctile(fincbtax_scle) if (ref_year==`year') , p(`num') 
}
}
gen str12 indicator=""
foreach year of numlist  1961 {
replace indicator = "pct_10_`year'" if (ref_year==`year' & fincbtax_scle>=incpct_5_`year' & fincbtax_scle<=incpct_15_`year')
replace indicator = "pct_50_`year'" if (ref_year==`year' & fincbtax_scle>=incpct_45_`year' & fincbtax_scle<=incpct_55_`year')
replace indicator = "pct_90_`year'" if (ref_year==`year' & fincbtax_scle>=incpct_85_`year' & fincbtax_scle<=incpct_95_`year')
}

drop incpct_*
*Create indicator for house ownership
gen home_own = 0
replace home_own = 1 if (ref_year==1961 & cutenure==1) 
replace home_own = 1 if (ref_year>=1970 & ref_year!=. & (cutenure==1 | cutenure==2 | cutenure==3))
*create indicator for cars that will be consistent for all years 
gen vehq_cons = 0 if (vehq==0) 
replace vehq_cons = 1 if (vehq==1)
replace vehq_cons = 2 if (vehq>=2 & vehq!=.)

foreach year of numlist  1961 { 

preserve 
keep if indicator=="pct_10_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by (ref_year)
gen percentile=10
save "`temp10'" , replace 
restore 

preserve 
keep if indicator=="pct_50_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by(ref_year)
gen percentile=50
save "`temp50'" , replace 
restore 

preserve 
keep if indicator=="pct_90_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by(ref_year)
gen percentile=90

append using `temp50' 
append using `temp10'
save inc_pct`year' , replace 
restore 
}

use inc_pct_allyears , clear 
append using inc_pct1961
save inc_pct_allyears , replace 


*1980s
use inequality_data_for_80 , clear 
drop if (ref_year==1983 | ref_year==1984)
keep if respstat==1
****CREATE COMPONENT PERCENTILES 
*Create transportation total variable with ONLY transportation expenditures included in core consumption measures
gen trans_tot1=.
replace trans_tot1 = (n_tot_vflow1/4)+gas_mo if (ref_year<=1979)
replace trans_tot1 = n_tot_vflow1+gas_mo if (ref_year>=1980)

*create housing + utilities expenditures measure 
gen hous_util_tot = hous_tot+util_tot
*create vehicle total
gen vehic_tot =.
replace vehic_tot = n_tot_vflow1/4 if (ref_year<=1979)
replace vehic_tot = n_tot_vflow1 if (ref_year>=1980)

*scale all component measures 
gen trans_scle = trans_tot1/scale
gen hous_scle = hous_tot/scale
gen fdhome_scle = fdhome_tot/scale 
gen hous_util_scle = hous_util_tot/scale 
gen vehic_scle = vehic_tot/scale


*convert to 2011 dollars 
foreach var of varlist trans_scle hous_scle fdhome_scle hous_util_scle vehic_scle {
gen r_`var'=cpi_u_rs_adj_11*`var' 
}

*Generate scaled income 
gen double fincbtax_scle = fincbtax/scale 

*Create indicators of percentile of core consumption by year
foreach year of numlist 1980 1981 {
foreach num of numlist 5 15 45 55 85 95 {
egen incpct_`num'_`year' = pctile(fincbtax_scle) if (ref_year==`year') , p(`num') 
}
}
gen str12 indicator=""
foreach year of numlist  1980 1981 {
replace indicator = "pct_10_`year'" if (ref_year==`year' & fincbtax_scle>=incpct_5_`year' & fincbtax_scle<=incpct_15_`year')
replace indicator = "pct_50_`year'" if (ref_year==`year' & fincbtax_scle>=incpct_45_`year' & fincbtax_scle<=incpct_55_`year')
replace indicator = "pct_90_`year'" if (ref_year==`year' & fincbtax_scle>=incpct_85_`year' & fincbtax_scle<=incpct_95_`year')
}

drop incpct_*
*Create indicator for house ownership
gen home_own = 0
replace home_own = 1 if (ref_year==1961 & cutenure==1) 
replace home_own = 1 if (ref_year>=1970 & ref_year!=. & (cutenure==1 | cutenure==2 | cutenure==3))
*create indicator for cars that will be consistent for all years 
gen vehq_cons = 0 if (vehq==0) 
replace vehq_cons = 1 if (vehq==1)
replace vehq_cons = 2 if (vehq>=2 & vehq!=.)

foreach year of numlist  1980 1981 { 

preserve 
keep if indicator=="pct_10_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by (ref_year)
gen percentile=10
save "`temp10'" , replace 
restore 

preserve 
keep if indicator=="pct_50_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by(ref_year)
gen percentile=50
save "`temp50'" , replace 
restore 

preserve 
keep if indicator=="pct_90_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by(ref_year)
gen percentile=90

append using `temp50' 
append using `temp10'
save inc_pct`year' , replace 
restore 
}

use inc_pct_allyears , clear 
append using inc_pct1980
append using inc_pct1981
sort ref_year percentile 
save inc_pct_allyears , replace 

export excel using income_pctiles , firstrow(var) replace 

log close 
*fin 
